package com.rjj.wlqq.multipleFileUp;


import com.jfinal.upload.MultipartRequest;
import com.jfinal.upload.UploadFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 多文件上传
 *
 * 后台接收的方式
 */
@Controller
public class MultipleFileUpController {
    Logger log = LoggerFactory.getLogger(MultipleFileUpController.class);

    /**
     * 进入多文件上传测试页面
     */
    @RequestMapping("/multipleFileUpload")
    public String multipleFile(){
      return "/multipleFileUp/multiplePicture.html";
    }

    /**
     * 多文件上传的后台接收器
     * @param fileList 前端传过来的文件
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Map<String, String> upload(@RequestParam("uploadFile") List<MultipartFile>  fileList, HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<String> keySet = parameterMap.keySet();
        for (String key : keySet) {
            String[] arr = parameterMap.get(key);
            if(arr.length == 0){
                log.info("参数一个：" + arr[0]);
            }else{
                for (String val : arr) {
                    log.info("参数多个：" + val);
                }
            }
        }

        //上传的文件
        MultipartHttpServletRequest request1 = (MultipartHttpServletRequest) request;
        MultiValueMap<String, MultipartFile> multiFileMap = request1.getMultiFileMap();
        List<MultipartFile> multipartFiles = multiFileMap.get("uploadFile");
        for (MultipartFile multipartFile : multipartFiles) {
            String originalFilename = multipartFile.getOriginalFilename();
            log.info("文件名称" + originalFilename);
            String filePath = null;
            try {
                filePath = ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath() + "/upload/img" + File.separator;
                log.info("获取文件路径：" + ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX));
                log.info("获取文件路径：" + ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath());
                log.info("获取文件路径：" + new File(filePath).getAbsolutePath());
                log.info("获取文件路径：" + filePath);
                //获取文件路径：file:/Users/renjianjun/study/ideaWorkSpace/demo/target/classes/
                //获取文件路径：/Users/renjianjun/study/ideaWorkSpace/demo/target/classes/
                //获取文件路径：/Users/renjianjun/study/ideaWorkSpace/demo/target/classes/upload/img
                //获取文件路径：/Users/renjianjun/study/ideaWorkSpace/demo/target/classes//upload/img/
                long time = System.currentTimeMillis();
                String endTag = time + originalFilename.substring(originalFilename.indexOf("."));
                File dest = new File(new File(filePath).getAbsolutePath() + File.separator + endTag);

                //创建文件
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }

                //b保存文件
                multipartFile.transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //响应
        Map<String,String> m = new HashMap<>(1);
        m.put("code","ok");
        return m;
    }

}
